﻿using QuanLySieuThi.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLySieuThi.Controllers
{

    public class BaseController<T>
        where T : class, new()
    {
        protected DataAccess db = new DataAccess();
        protected Helper<T> helper = new Helper<T>();

 

        public List<T> DanhSach
        {
            get
            {
                string nameType = t.GetType().Name;
                return helper.TableToList(db.ExecuteQuery("select * from " + nameType));
            }
        }
        private static T t = new T();
        private string typeName { get { return t.GetType().Name; } }



        public DataTable GetDataTable
        {
            get
            {
                string nameType = t.GetType().Name;
                return db.ExecuteQuery("select * from " + nameType);
            }
        }

        public T LayTheoMa(string name)
        {
            string fieldName = "Ma" + t.GetType().Name;
            return DanhSach.Where(item => item.GetType().GetProperty(fieldName).GetValue(item).Equals(name)).FirstOrDefault();
        }

        public virtual void Delete(string ma)
        {
            //Xóa hóa đơn nhập và hóa đơn xuất
            db.ExecuteNonQuery("delete "+typeName+" where Ma"+typeName+" = '"+ma+"'");
        }

        public T LayTheoTen(string name)
        {
            string fieldName = "Ten" + t.GetType().Name;
            return DanhSach.Where(item => item.GetType().GetProperty(fieldName).GetValue(item).Equals(name)).FirstOrDefault();
        }

        public void Add(T t)
        {
            string nameProc = "Them" + t.GetType().Name;
            List<SqlParameter> list = new List<SqlParameter>();
            foreach (var item in t.GetType().GetProperties())
                list.Add(new SqlParameter("@" + item.Name, item.GetValue(t)));
            db.ExecuteNonQuery(nameProc, list.ToArray());
        }

        public void Update(T t)
        {
            string nameProc = "Update" + t.GetType().Name;
            List<SqlParameter> list = new List<SqlParameter>();
            foreach (var item in t.GetType().GetProperties())
                list.Add(new SqlParameter("@" + item.Name, item.GetValue(t)));
            db.ExecuteNonQuery(nameProc, list.ToArray());
        }

        public DataTable TimKiemTheoMa(string ma)
        {
            return db.ExecuteQuery("select * from " + typeName + " where Ma" + typeName + " like '%" + ma + "%'");
        }

        public DataTable TimKiemTheoTen(string ten)
        {
            return db.ExecuteQuery("select * from " + typeName + " where Ten" + typeName + " like '%" + ten + "%'");
        }

        public DataTable TimKiem(string key)
        {
            return db.ExecuteQuery("select * from " + typeName + " where Ten" + typeName + " like '%" + key + "%' or Ma"+typeName+" like '%"+key+"%'");
        }
    }
}
